//
//  NetworkTool.swift
//  ListenFM
//
//  Created by szq on 2017/8/8.
//  Copyright © 2017年 胡定锋. All rights reserved.
//

import Foundation
import Alamofire
import SwiftyJSON

enum MethodType{
    
    case GET
    case POST
}

// 一次封装
class NetworkTool{
    
    
    class func requestDataToJSON(_ type: MethodType = .POST,URLString: String,params: [String:Any]?,success:@escaping (_ responseObject:[String:Any])->(),failture:@escaping (_ error:NSError)->()){
        
        // 获取类型
        let method = type == .GET ? HTTPMethod.get : HTTPMethod.post
        // 发送网络请求
        Alamofire.request(URLString, method: method, parameters: params).responseJSON { (response) in
            switch response.result {
            case.success:
                if let value = response.result.value as? [String : AnyObject] {
                    success(value)
                    
                }
            case .failure(let error):
                failture(error as NSError)
            }
        }
        
    }
    
    /// 请求方法 返回值String
    class func requestDataToString(_ type : MethodType = .POST, URLString : String, params : [String : Any]?,success : @escaping (_ responseObject : String)->(), failture : @escaping (_ error : NSError)->()) {
        
        // 1.获取类型
        let method = type == .POST ? HTTPMethod.get : HTTPMethod.post
        
        // 2.发送网络请求
        Alamofire.request(URLString, method: method, parameters: params).responseString { (response) in
            switch response.result {
            case.success:
                if let value = response.result.value{
                    success(value)
                }
            case .failure(let error):
                failture(error as NSError)
            }
        }
        
    }
    
}


// 二次封装
extension NetworkTool{
    
    // GET请求  返回JSON
    class func GETJSON(URLString : String, params : [String : Any]?,success : @escaping (_ responseObject : [String:Any])->(), failture : @escaping (_ error : NSError)->()) {
        NetworkTool.requestDataToJSON(.GET, URLString: URLString, params: params, success: success, failture: failture)
    }
    
    // POST请求  返回JSON
    class func POSTJSON(URLString : String, params : [String : Any]?,success : @escaping (_ responseObject :[String:Any])->(), failture : @escaping (_ error : NSError)->()) {
        NetworkTool.requestDataToJSON(.POST, URLString: URLString, params: params,success: success, failture: failture)
    }
    
    
    // GET请求 返回String
    class func GET(URLString : String, params : [String : Any]?,success : @escaping (_ responseObject : String)->(), failture : @escaping (_ error : NSError)->()) {
        NetworkTool.requestDataToString(.GET, URLString: URLString, params: params, success: success, failture: failture)
    }
    
    
    // POST请求 返回String
    class func POST(URLString : String, params : [String : Any]?,success : @escaping (_ responseObject : String)->(), failture : @escaping (_ error : NSError)->()) {
        NetworkTool.requestDataToString(.POST, URLString: URLString, params: params, success: success, failture: failture)
        
    }
    
}








